package com.chance.cc.crawler.prod.command.job.domain.news.zhihu;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterInfo;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.news.NewsCommonScript;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * created by CC on 2020/11/26
 * mail 279020185@qq.com
 */
public class ZHSearchRealtimeCrawlerScheduleJob extends NewsCommonScript {

    public static final String domain = "zhihu";

    public static void main(String[] args) {
//        publishCrawlerScheduleJobInfo();
        System.out.println(publishCrawlerScheduleJobInfo().generateCrawlerTriggerKey());
    }

    public static CrawlerJob publishCrawlerScheduleJobInfo(){


        CrawlerJob jdCrawlerJob = crawlerJob();

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(jdCrawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("知乎搜索关键词定时采集");
        crawlerScheduleJob.setCrawlerKey(jdCrawlerJob.generateCrawlerKey());
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());
        return jdCrawlerJob;

    }

    /**
     * 17765187595
     * chancedata123
     * chance1
     * taobao采集
     * @return
     */
    public static CrawlerJob crawlerJob() {
        FilterInfo filterInfo = new FilterInfo();
        filterInfo.setFilter(CrawlerEnum.CrawlerRecordFilter.count);
        filterInfo.setCountTotalNum(100);

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .httpUrl("https://www.zhihu.com/api/v4/search_v3")
                .httpHead("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36")
                .httpHead("cookie","d_c0=\"ADBiHqMywQ6PTlAq-wLKq4redBYLDaR06Hw=|1546263453\"; _xsrf=kYQKJQ39BrawjmneAv07aXGrmZhmHvaz; _ga=GA1.2.1328271267.1582701249; _zap=61c8630f-164c-4912-b2e3-11b8a48cbb83; __snaker__id=1wqY9sxpGpxAdxuU; _9755xjdesxxd_=32; YD00517437729195%3AWM_TID=i0Pf3X5wrfJERAFVBVM7kI4V7gwbITFX; tst=r; q_c1=8a20d8ca018d48a9be837a64a594eb25|1617877410000|1575450284000; __utma=51854390.1328271267.1582701249.1600809316.1617877411.9; __utmz=51854390.1617877411.9.1.utmcsr=zhihu.com|utmccn=(referral)|utmcmd=referral|utmcct=/question/47696522/answer/1526610644; __utmv=51854390.110--|2=registration_date=20201028=1^3=entry_date=20191204=1; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1620614678,1620631069,1620631607,1620696524; captcha_session_v2=\"2|1:0|10:1620696617|18:captcha_session_v2|88:SlBhWHpHT0E4c3hZVXQxc2E3VzFudUZJSXZWRnNrVjJpUVFINFVHY1JWblRTR0tza0Q0WXZjTHJwUkp5cytheg==|1692a42907c3c647eeb6da63d0f3b092eae050f2ebe5976181e14290ee9ef412\"; gdxidpyhxdE=0IewXw51HUH4X%2BQmLar%5CM92OahrmUEcre%2FgvQ2uLPneQCm18sGOHrdvbC3hAINMKDEc%5Cby8caoxMk8LMs%5C%5Cgp595TdZmRZv0USVGEdjrMQZ%2FC4NficY9q7UZ%2Bv0Eldu0dMqx70m4UCBqgOZbcmvM0UpUbYptm%2F3vkoC8CnDJ9c57CKPW%3A1620697521306; YD00517437729195%3AWM_NI=ERriSNpFYUETsHTKfpb5Bw9l9tMI0So3CKGNisWpFvEB6YwLasd0PYhHPpkFipaQK6ATItFEwbIBToZ1hNMkLqqEvHx%2F1mWf4Do7IqzV54XLfJEnzC6VRQW%2FlXA8Fi03Qng%3D; YD00517437729195%3AWM_NIKE=9ca17ae2e6ffcda170e2e6ee88c65f97b7e189ef3ef8eb8fa3c45a978a9fafb574aebf8ca2e77fbbebb7d6ec2af0fea7c3b92a878ebe8ad246b49a97b6c860b1989cace57ca2eb8e98bc68fbac83aab359aded8dacc646abad9991c57e879efabaf648a3adacd1d353ac98a091ea4eabb0afb2ca3e838c8795c54fb5be87a3d7659aefa8afd06190afbfa6f544bcf1ff8ed66ef89ca1d8cc7bba8afbd5db48a6968b8ff949f68a9eb2d625b0888fd9c563f4ed9f8fe637e2a3; captcha_ticket_v2=\"2|1:0|10:1620696625|17:captcha_ticket_v2|704:eyJ2YWxpZGF0ZSI6IkNOMzFfV0pOTXpqaUxYS01jUmdZNFh1SkYuelVMbER3WmFONG95U3FtZGJNZXBONTVzTXFES1FOTzhJWVgxNlR2WXl3SzY1TUdNcmE5anVDd2hMbS1DWTdHb3BhT1pzOHdRNGNhTk56bFdGVUsuZkJyVlA2Q2xyTWEtdmNRRjBqTVZpa1VLaWc4YnNvQXo5NG1TMS5kQVFmanZCRXp6cHJvRTB1ZUR1Uk56UmFxeHdqXzdyWTVKeDRmTHFmeEhucWluOWFjbGdDQ29ka3dKQ0I5X0RZcHVRZTJJZldiLkJ3VzJITTh3Mjg0c0oyZURVOThIQ3F6S2QwRS00V3QwS0w3dWE4UlBxcEx1YmNpSHlNMVUuc05uRnJiLnQ4UnlZUnNjUHNESWk4eEV1Sk1wR05TYW9VVUhCaHpFaHNRcHZVWjFfejd0eFNGLllJOTJtRUJHVE1KbS12N2VNRDB6dTVDVW5WN2xrWDhmX2trU3JITy5TMlZfdnNWUERVVS1CLmxDSHlJOUtJYVQyMEgyQ0xydWstdmhTS2ZmaVpRcFVCakp4dkV0MWR4dE1iS0JJYW5WanNkWlJQWG9vZ25kc3B3OVlmZTVPenByT2Rab29RWWVmMFBoZ1l0bU1MZ1BtRW1henl2LUloZS1pVXdlTmlpR2RpMTU2ZEhZSFk4TV9YMyJ9|d15fb0ccbc99fa6ce94a926c0183f2bb4c3da99edb1697119d86af727be62f23\"; z_c0=\"2|1:0|10:1620696625|4:z_c0|92:Mi4xSnpwYklBQUFBQUFBTUdJZW96TEJEaVlBQUFCZ0FsVk5NU3lIWVFDRFFSc1NyRlRKa2l0WDkxcXdkZU9KeEwzdGh3|f8861905fee9219636434f75906c2b76002bb005f680c51374349f0d23fb6958\"; SESSIONID=ydQW3YxpZ7rnc99wRdU9I9lfySWSP1OatdCpZL12rKQ; JOID=UFkXAU2yBciuMFUzW7JC3jCL7rBKyGKZ_QcPSCvsMbnAV2gJL1KPV843VTBf0X9SWA7zoThx4jE19HepFS0NF0k=; osd=WlsSAk24B82tMF8xXrFC1DKO7bBAymea_Q0NTSjsO7vFVGgDLVeMV8Q1UDNf231XWw75oz1y4js38XSpHy8IFEk=; anc_cap_id=4416ccd21deb4822929ebca2b0a72f09; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1620810237; KLBRSID=b5ffb4aa1a842930a6f64d0a8f93e9bf|1620810239|1620806085")
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .filter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange)
                .turnPageFilterInfo(filterInfo)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(1,null))
                .addFilterInfo(FilterUtils.redisFilterKeyInfo("filter-zhihu-search-article-queue"))
                .proxy(proxy)
                .build();

        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addSite("search");
        requestRecord.tagsCreator().bizTags().addDomain(domain);

        CrawlerRecord articleFilterCrawlerRecord = new CrawlerRecord();
        articleFilterCrawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange);
        articleFilterCrawlerRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(1,null));
        articleFilterCrawlerRecord.addFilterInfo(FilterUtils.redisFilterKeyInfo("filter-zhihu-search-answer-queue"));
        requestRecord.tagsCreator().bizTags().addCustomKV("article_record_filter_info", JSON.toJSONString(articleFilterCrawlerRecord));

        CrawlerRecord commentFilterCrawlerRecord = new CrawlerRecord();
        commentFilterCrawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.key);
        commentFilterCrawlerRecord.addFilterInfo(FilterUtils.redisFilterKeyInfo("filter-zhihu-search-comment-queue"));
        requestRecord.tagsCreator().bizTags().addCustomKV("comment_record_filter_info", JSON.toJSONString(commentFilterCrawlerRecord));

        requestRecord.tagsCreator().bizTags().addCustomKV("time_zone", "a_day");
        requestRecord.tagsCreator().bizTags().addCustomKV("scheduler_type", "realTime");

        //初始 comment crawler request record
        String requestQueueName = StringUtils.joinWith("-","crawler",domain,"search","queue");
        CrawlerRequestRecord initNewsCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("zhihu_news",turnPageItem)
                .httpUrl(metaServiceHttpPrefix+"/v1/meta/zhihu/keys?site=search")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        return CrawlerJob.builder()
                .crawlerJobThreadNumber(15)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-","search",CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                .kafkaResultPipeline(null,kafkaTopic,null)
                .requestRecord(requestRecord)
                .supportRecord(initNewsCrawlerRecord)
                .build();
    }

}
